home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Graphics / QuickDraw GX / GX->PostScript Sample / GXToPostScript / Imaging Engine / FixGraphicsState.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.3 KB  |  227 lines  |  [TEXT/MPS ]

  1. /*
  2.      File:        FixGraphicsState.c
  3.  
  4.      Contains:    QuickDraw GX to PostScript conversion code.
  5.                          File contains routines for rebuilding
  6.                         the graphics state after save/restore things
  7.                         happen
  8.  
  9.      Version:    Technology:    Quickdraw GX 1.1.x
  10.       
  11.      Copyright:    © 1990-1997 by Apple Computer, Inc., all rights reserved.
  12. */
  13.  
  14. #include "GXToPSBuildConfig.h"
  15. #include <GXGraphics.h>
  16. #include "GXGraphicsPriv.h"
  17. #include <GXEnvironment.h>
  18. #include "GXToPostScript.h"
  19. #include "IOUtilities.h"
  20. #include "RDUtil.h"
  21. #include "FontHandler.h"
  22. #include "PublicPostScriptIE.h"
  23. #include "private.h"
  24. #include "PSIEResources.h"
  25. #include "GXErrors.h"
  26. #include "ShapeUtilities.h"
  27.  
  28. #ifdef resumeLabel
  29.     #undef resumeLabel
  30. #endif
  31. #define resumeLabel(exception)
  32.  
  33.  
  34. typedef struct {
  35.     gxTransform        theTransform;
  36.     gxInk                    theInk;
  37. } TSavedState;
  38.  
  39. /***************************************************
  40.  
  41.     Function: FixGraphicsState
  42.     
  43.     Function sees to it that the printer's nested graphics state
  44.     agrees with the Imaging Engine's internal nested graphics state.
  45.     The printer's state gets corrupted due to the somebody doing 
  46.     saves and restores (such as the font handler)
  47.     
  48.     hGlobals:            Handle to the imaging engine globals
  49.     didRestore:        Boolean indicating that a restore was done.
  50.     didSave:            Boolean indicating that a save was done.
  51.     
  52.     If both didSave and didRestore are true, then it is assumed that
  53.     the order was restore first and then save.  
  54.         (doing a save and then a restore would accomplish nothing)
  55.     
  56. *****************************************************/
  57. OSErr FixGraphicsState(TIEGlobalsHdl hGlobals, Boolean didRestore, Boolean didSave)
  58.     {
  59.         OSErr                    status = noErr;
  60.         long                    saveDepth;
  61.         TIEGlobalsPtr    pGlobals;
  62.         TGstate                *pGstate;
  63.         TSavedState        *savedGstates, *pSavedGstate;
  64.         long                    i, depth;
  65.  
  66.  
  67.         /*** If only a save was done, mark it in the graphics state ***/
  68.         if (didSave && !didRestore) {
  69.         
  70.             pGlobals = *hGlobals;
  71.             pGlobals->gStateNest[pGlobals->gStateDepth].flags |= eGstateDidSave;
  72.             
  73.             #if DEBUGLEVEL >= DEBUGFEEDBACK
  74.                 dprintf(trace, "Save done at level %d", pGlobals->gStateDepth);
  75.             #endif
  76.         
  77.         } else if (didRestore) {
  78.         
  79.             /*** Find the depth of the save ***/
  80.             saveDepth = 0;
  81.             pGlobals = *hGlobals;
  82.             pGstate = &(pGlobals->gStateNest[0]);
  83.             depth = pGlobals->gStateDepth;
  84.             
  85.             while (!(pGstate->flags & eGstateDidSave)) {
  86.                 
  87.                 ++pGstate;
  88.                 ++saveDepth;
  89.                 
  90.                 #if DEBUGLEVEL > 1
  91.                     if (saveDepth > pGlobals->gStateDepth) {
  92.                         DebugStr("\pFatal Error, Imaging Engine told to do a restore when there was no save noted");
  93.                         return(-999);
  94.                     }//end if                
  95.                 #endif
  96.                 
  97.             }//end while
  98.             
  99.             #if DEBUGLEVEL >= DEBUGFEEDBACK
  100.             dprintf(trace, "Restoring to level %d, Max: %d, didSave: %d", saveDepth, depth, didSave);
  101.             #endif 
  102.             
  103.             /*** If a save wasn't done, mark didSave for this level false ***/
  104.             if (!didSave)
  105.                 pGstate->flags &= ~eGstateDidSave;
  106.             
  107.             
  108.             /*** 
  109.                 Now output the graphics state from the save level on
  110.             ***/
  111.             if (saveDepth < depth) {
  112.             
  113.                 /* Make copies of the graphics states from 1 after saveDepth to the end */
  114.                 status = PSSetWorkSpaceSize(hGlobals, (depth - saveDepth) * sizeof(TSavedState));
  115.                 nrequire(status, failed_WorkSpace);
  116.     
  117.                 HLockHi((Handle)hGlobals);                    // In case graphics calls move memory.
  118.                 HLockHi((*hGlobals)->hWorkSpace);
  119.                 pGlobals = *hGlobals;
  120.                 savedGstates = (TSavedState*)(*(pGlobals->hWorkSpace));
  121.                 pSavedGstate = savedGstates;
  122.                 pGstate = &(pGlobals->gStateNest[saveDepth+1]);
  123.                 for (i = saveDepth+1; i <= depth; ++i) {
  124.  
  125.                     pSavedGstate->theInk = pGstate->theInk;
  126.                     if (pSavedGstate->theInk != nil) 
  127.                         GXCloneInk(pSavedGstate->theInk);
  128.  
  129.                     pSavedGstate->theTransform = pGstate->theTransform;
  130.                     if (pSavedGstate->theTransform != nil)
  131.                         GXCloneTransform(pSavedGstate->theTransform);
  132.                     
  133.                     ++pSavedGstate;
  134.                     ++pGstate;
  135.                     
  136.                 }//end for
  137.     
  138.                 
  139.                 /******
  140.                     Now do internal grestores of our state back to the save level
  141.                         We do this because the routines that affect the output graphics state
  142.                         such as TransformDrone and ColorPrimitive check against the stored, nested
  143.                         graphics state which we are trying to recreate on the printer.  So the easiest
  144.                         way to re-create it on the printer since it has been destroyed on the printer
  145.                         is to destroy the internal copy of it as well and then regenerated in the 
  146.                         normal manner.
  147.                 ********/
  148.                 for (i = saveDepth+1; i <= depth; ++i) {
  149.                 
  150.                     status = DoGrestore(hGlobals, true);
  151.                     nrequire(status, failed_Grestore);
  152.                 
  153.                 }//end for
  154.                 
  155.                 /*** Now regenerate the graphics states from saveDepth + 1 on ****/
  156.                 
  157.                 (*hGlobals)->ieStateFlags |= eInitializeGstate;
  158.                 
  159.                 pSavedGstate = savedGstates;
  160.                 for (i = saveDepth + 1; i <= depth; ++i) {
  161.                 
  162.                     /* Do a gsave and output the transform */
  163.                     
  164.                     nrequire(status = DoGsave(hGlobals, false), failed_Gsave);
  165.                     nrequire(status = TransformDrone(hGlobals, pSavedGstate->theTransform), failed_Transform);
  166.                     
  167.                     #if DEBUGLEVEL >= DEBUGFEEDBACK
  168.                         {
  169.                             unsigned char pointsLeft[] = "%- points left: ";
  170.                             TRDParams*        rdParams = pGlobals->pRDParams;
  171.                             nrequire(status = PSIEBufferData(hGlobals, pointsLeft, 16, 0), failed_Transform);
  172.                             pGlobals = *hGlobals;
  173.                             rdParams->resIndex = kDoInt;
  174.                             nrequire(status = RDResPrintf(rdParams, pGlobals->gStateNest[pGlobals->gStateDepth].pointsAvail), failed_Transform);
  175.                         }
  176.                     #endif
  177.                     
  178.                     /* Now do the ink for this level */
  179.                     
  180.                     status = InkDrone(hGlobals, pSavedGstate->theInk);
  181.                     nrequire(status, failed_Ink);
  182.                     
  183.                     /** And dispose of the transform and ink we copied **/
  184.                     GXDisposeInk(pSavedGstate->theInk);
  185.                     GXDisposeTransform(pSavedGstate->theTransform);
  186.                     
  187.                     ++pSavedGstate;
  188.                 
  189.                 }//end for
  190.                             
  191.                                 
  192. failed_Ink:
  193. failed_Transform:    
  194. failed_Gsave:
  195. failed_Grestore:        
  196.                 {
  197.                     OSErr        saveStatus = PSReleaseWorkSpace(hGlobals);
  198.                     if (saveStatus != noErr) status = saveStatus;
  199.                 }
  200.  
  201.             }//end if
  202.  
  203.             /*** Now recreate the Graphics State style stuff ***/
  204.             if (status == noErr) {
  205.             
  206.                 (*hGlobals)->ieStateFlags |= (eStyleOutOfDate + eFontOutOfDate);
  207.             
  208.                 status = StyleDrone(hGlobals, (*hGlobals)->theStyle);
  209.                 nrequire(status, failed_Style);
  210.                 
  211.             }//end if
  212.         
  213.         }//end if
  214.  
  215.  
  216.             
  217. failed_Style:
  218.  
  219. failed_WorkSpace:        
  220.  
  221.         return(status);
  222.         
  223.     }//FixGraphicsState
  224.  
  225.  
  226.  
  227.